package com.xinongbest.mall.controller;

import com.xinongbest.mall.common.ApiRestResponse;
import com.xinongbest.mall.common.Constant;
import com.xinongbest.mall.exception.ImoocMallExceptionEnum;
import com.xinongbest.mall.model.dto.CategoryDto;
import com.xinongbest.mall.model.dto.UpdateCategoryDto;
import com.xinongbest.mall.model.pojo.User;
import com.xinongbest.mall.model.vo.CategoryVo;
import com.xinongbest.mall.service.CategoryService;
import com.xinongbest.mall.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

/**
 * @author lemonwan
 * 商品分类控制器
 */
@Controller
public class CategoryController {

    @Autowired
    UserService userService;

    @Autowired
    CategoryService categoryService;

    @ApiOperation("后台-添加商品分类")
    @PostMapping("/admin/category/add")
    @ResponseBody
    public ApiRestResponse addCategory(HttpSession session, @Valid @RequestBody CategoryDto categoryDto) {
        User currentUser = (User) session.getAttribute(Constant.IMOOC_MALL_USER);
        if (currentUser == null) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_LOGIN);
        } else {
            if (!userService.checkAdminRole(currentUser)) {
                return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_ADMIN);
            }
        }
        categoryService.add(categoryDto);
        return ApiRestResponse.success();
    }

    @PostMapping("/admin/category/update")
    @ResponseBody
    public ApiRestResponse updateCategory(HttpSession session, @Valid @RequestBody UpdateCategoryDto updateCategoryDto) {
        User currentUser = (User) session.getAttribute(Constant.IMOOC_MALL_USER);
        if (currentUser == null) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_LOGIN);
        } else {
            if (!userService.checkAdminRole(currentUser)) {
                return ApiRestResponse.error(ImoocMallExceptionEnum.NEED_ADMIN);
            }
        }
        categoryService.update(updateCategoryDto);
        return ApiRestResponse.success();
    }

    @PostMapping("/admin/category/delete")
    @ResponseBody
    public ApiRestResponse deleteCategory(@RequestParam Integer id) {
        categoryService.delete(id);
        return ApiRestResponse.success();
    }

    @PostMapping("/admin/category/list")
    @ResponseBody
    public ApiRestResponse categoryListForAdmin(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
        return ApiRestResponse.success(categoryService.listForAdmin(pageNum, pageSize));
    }

    @PostMapping("/category/list")
    @ResponseBody
    public ApiRestResponse categoryListForConsumer() {
        List<CategoryVo> categoryVos = categoryService.listForConsumer(0);
        return ApiRestResponse.success(categoryVos);
    }

}
